package com.sunny.ynhz.ui.patrol.problems;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.just.library.AgentWeb;
import com.sunny.ynhz.R;
import com.sunny.ynhz.api.NetUrl;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;
import com.sunny.ynhz.utils.MD5Utils;
import com.sunny.ynhz.utils.SPUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import butterknife.BindView;

/**
 * Created by luao on 2017/8/4.
 */

public class OfficeAdminActivity2 extends AppBaseTranslationTitleBarActivity {


    private AgentWeb mAgentWeb;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_web);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        String url = NetUrl.baseUrl + "pages/problem/selectRiverHeight.jsp?addvcd=" + SPUtil.get("addvcd", "")+"&userId="+SPUtil.get("userId", "")+"&userType="+SPUtil.get("userType", "");
        if (url.indexOf("?") != -1) {
            url = url + "&token=" + MD5Utils.string2MD5((System.currentTimeMillis() * 5 - 1799) + "") + "&tokentime=" + System.currentTimeMillis();
        } else {
            url = url + "?token=" + MD5Utils.string2MD5((System.currentTimeMillis() * 5 - 1799) + "") + "&tokentime=" + System.currentTimeMillis();
        }

        Log.e("URL111", "URL = " + url);

        WebViewClient mWebViewClient = new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                try {
                    url = URLDecoder.decode(url, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (url.contains("baidu")) {
                    String id = getValueByName(url, "id");
                    String name = getValueByName(url, "name");
                    Log.e("URL111", "URL = " + url + "     id = " + id + "    name = " + name);
                    Intent intent = new Intent();
                    intent.putExtra("id", id);
                    intent.putExtra("name", name);
                    setResult(500, intent);
                    finish();
                } else {
                    super.onPageStarted(view, url, favicon);
                    Log.e("URL111", "URL = " + url);
                }
            }
        };

        mAgentWeb = AgentWeb.with(this)//
                .setAgentWebParent(linearLayout, new LinearLayout.LayoutParams(-1, -1))//
                .useDefaultIndicator()//
                .defaultProgressBarColor()
                .setWebViewClient(mWebViewClient)
                .setSecutityType(AgentWeb.SecurityType.strict)
                .createAgentWeb()//
                .ready()
                .go(url);
    }

    public static String getValueByName(String url, String name) {
        String result = "";
        int index = url.indexOf("?");
        String temp = url.substring(index + 1);
        String[] keyValue = temp.split("&");
        for (String str : keyValue) {
            if (str.contains(name)) {
                result = str.replace(name + "=", "");
                break;
            }
        }
        return result;
    }
}
